﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using OpalisConsole.EndUserService;

namespace OpalisConsole.Views.User_Imaging
{
    public partial class Registration : Page
    {
        public Registration()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.Current.Resources["Computer_ID"].Equals("Not Approved"))
            {
                this.NavigationService.Navigate(new System.Uri("/User_Imaging/LandingPage", System.UriKind.Relative));
            }
            else
            {
                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.getApprovalRequestStatusCompleted += new EventHandler<getApprovalRequestStatusCompletedEventArgs>(proxy_getApprovalRequestStatusCompleted);
                proxy.getApprovalRequestStatusAsync(App.Current.Resources["ComputerName"].ToString(), App.Current.Resources["UserID"].ToString());
            }
        }

        private void setupNewRequestMessage()
        {
            txtRegistrationHeader.Text = "Welcome to the General Mills Information Systems Fresh Start Process!";
            txtRegistrationBody.TextWrapping = TextWrapping.Wrap;
            txtRegistrationBody.Text = "Fresh Start will upgrade (or refresh) this computer to the GMI standard Windows 7, Office 2010, " +
                                       "Internet Explorer 8 software. It will also assist you with understanding what additional software " + 
                                       "you are licensed to use under Windows 7 and what can and cannot be re-installed.\n" +
                                       "\n" +
                                       "The first step is to register this computer with the Fresh Start system. The registration process " + 
                                       "will assure this computer meets certain hardware and software requirements before you schedule the " + 
                                       "update. You will receive a scheduling invitation via email when your computer is approved " + 
                                       "(normally in less than 3 hours). If there are questions about your setup, a technician will call you\n" +
                                       "\n" +
                                       "This computer’s name is: [" + App.Current.Resources["ComputerName"].ToString() + "]!\n" + 
                                       "\n" + 
                                       "Click \"Request Authoirzation\" to begin.";
                                
            btnRegister.Visibility = System.Windows.Visibility.Visible;
        }
        private void setupPendingRequestMessage()
        {
            txtRegistrationHeader.Text = "Registration - Request is Pending";
            txtRegistrationBody.Text = "Welcome to the Windows 7 Fresh Start Process!\n" +
                                       "\n" +
                                       "You have successfully sumitted an authorization request.\n" +
                                       "The Fresh Start team may take up to two days to approve your request, thank you for your patience!";
            btnRegister.Visibility = System.Windows.Visibility.Collapsed;
        }

        void proxy_getApprovalRequestStatusCompleted(object sender, getApprovalRequestStatusCompletedEventArgs e)
        {
            if (e.Result.ToString().Equals(String.Empty))
            {
                setupNewRequestMessage();
            }
            else
            {
                setupPendingRequestMessage();
            }
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
            proxy.submitApprovalRequestCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_submitApprovalRequestCompleted);
            proxy.submitApprovalRequestAsync(App.Current.Resources["ComputerName"].ToString(), App.Current.Resources["UserID"].ToString());
        }

        void proxy_submitApprovalRequestCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
            proxy.getApprovalRequestStatusCompleted += new EventHandler<getApprovalRequestStatusCompletedEventArgs>(proxy_getApprovalRequestStatusCompleted);
            proxy.getApprovalRequestStatusAsync(App.Current.Resources["ComputerName"].ToString(), App.Current.Resources["UserID"].ToString());
            FinishedWindow window = new FinishedWindow("Your request has been successfully submitted");
            window.Show();
        }

        private void registrationVideoImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("http://spis.generalmills.com/sites/mshub/ContentLibrary/FreshStart01.wmv"), "_newWindow");
        }
    }
}
